#include "ej_fm1505_phy.h"
#include "board_includes.h"
#include "common_includes.h"

#ifdef SUPPORT_FM1505_DMS


#define FM1505_CS_PIN			(FM1505_SPI_CS)
#define FM1505_CS_INIT()		{eBspGpioConfig(FM1505_CS_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_CS_PIN, 1);}
#define FM1505_CS_HIGH()		{eBspGpioConfig(FM1505_CS_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_CS_PIN, 1);}
#define FM1505_CS_LOW()			{eBspGpioConfig(FM1505_CS_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_CS_PIN, 0);}

#define FM1505_POWER_PIN		(FM1505_CHIP_POWER)
#define FM1505_POWER_INIT()		{eBspGpioConfig(FM1505_POWER_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_POWER_PIN, 1);}
#define FM1505_POWER_HIGH()		{eBspGpioConfig(FM1505_POWER_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_POWER_PIN, 1);}
#define FM1505_POWER_LOW()		{eBspGpioConfig(FM1505_POWER_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_POWER_PIN, 0);}

//低电平复位
#define FM1505_RESET_PIN		(FM1505_CHIP_RESET)
#define FM1505_RESET_INIT()		{eBspGpioConfig(FM1505_RESET_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_RESET_PIN, 1);}
#define FM1505_RESET_ENABLE()	{eBspGpioConfig(FM1505_RESET_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_RESET_PIN, 1);}
#define FM1505_RESET_DISABLE()	{eBspGpioConfig(FM1505_RESET_PIN, E_BSP_GPIO_OUTPUT, E_BSP_GPIO_NOPULL);eBspGpioWritePin(FM1505_RESET_PIN, 0);}

void ej_fm1505_phy_dly_ms(uint32_t dly_ms)
{
	udelay(1000*dly_ms);
}

void ej_fm1505_phy_chip_reset_control(bool enable)
{
	if (enable)
	{
		FM1505_RESET_DISABLE();	//低电平复位
	}
	else
	{
		FM1505_RESET_ENABLE();
	}
}

void ej_fm1505_phy_spi_cs_control(bool enable)
{
	if (enable)
	{
		FM1505_CS_HIGH();
	}
	else
	{
		FM1505_CS_LOW();
	}	
}

void ej_fm1505_phy_spi_write(uint8_t *wbuf, uint16_t len)
{
	ej_bsp_spi_write(FM1505_DEV_NAME,wbuf, len);
}

void ej_fm1505_phy_spi_read(uint8_t *rbuf, uint16_t len)
{
	ej_bsp_spi_read(FM1505_DEV_NAME,rbuf, len);
}

void ej_fm1505_phy_init(void)
{
	FM1505_POWER_INIT();
	FM1505_POWER_HIGH();

	FM1505_RESET_INIT();
	FM1505_RESET_ENABLE();

	ej_bsp_spi_attach_device(UCFG_SPI0_NAME, FM1505_DEV_NAME, FM1505_SPI_CS);
}

void ej_fm1505_phy_exit(void)
{
	FM1505_POWER_LOW();
}
#endif
